clear all
set more off
cd ""

use "sample.dta", clear

**********
* Panel A
**********

* Generate time-to-event dummies
* Transformation to get linear combinations of parameters

tab rel_time, gen(rel_w)
gen rel_w11_21 = rel_w11*21
forvalue i = 12/31 {
gen diff_rel`i'_11 = rel_w`i' - rel_w11
}

reghdfe in_confn14 rel_w11_21 diff_rel* wdsp temp prcp, absorb(i.city_code i.time i.prov_code#c.time) cluster(time)

reghdfe in_confn10 rel_w11_21 diff_rel* wdsp temp prcp, absorb(i.city_code i.time i.prov_code#c.time) cluster(time)

reghdfe in_confn7 rel_w11_21 diff_rel* wdsp temp prcp, absorb(i.city_code i.time i.prov_code#c.time) cluster(time)

reghdfe in_confn3 rel_w11_21 diff_rel* wdsp temp prcp, absorb(i.city_code i.time i.prov_code#c.time) cluster(time)

**********
* Panel B
**********

* Generate interaction terms between time-to-event dummies and faction
* Transformation to get linear combinations of parameters

forvalue i = 1/31 {
gen rel_w`i'_D = rel_w`i' * faction
}
gen rel_w11_D_21 = rel_w11_D*21
forvalue i = 12/31 {
gen diff_Drel`i'_11 = rel_w`i'_D - rel_w11_D
}

reghdfe in_confn14 rel_w11_D_21 diff_Drel* treat_csm wdsp temp prcp, absorb(i.city_code#i.faction i.time#i.faction i.prov_code#c.time#i.faction) cluster(time)

reghdfe in_confn10 rel_w11_D_21 diff_Drel* treat_csm wdsp temp prcp, absorb(i.city_code#i.faction i.time#i.faction i.prov_code#c.time#i.faction) cluster(time)

reghdfe in_confn7 rel_w11_D_21 diff_Drel* treat_csm wdsp temp prcp, absorb(i.city_code#i.faction i.time#i.faction i.prov_code#c.time#i.faction) cluster(time)

reghdfe in_confn3 rel_w11_D_21 diff_Drel* treat_csm wdsp temp prcp, absorb(i.city_code#i.faction i.time#i.faction i.prov_code#c.time#i.faction) cluster(time)

******************************
* Counterfactual calculations
******************************

qui: reghdfe in_confn14 rel_w11_D_21 diff_Drel* treat_csm wdsp temp prcp, absorb(i.city_code#i.faction i.time#i.faction i.prov_code#c.time#i.faction) cluster(time)
estimate store DDD

predict yhat, xb
gen rhat = in_confn14 - yhat

* Prediction if all factions were 1

gen faction_s = faction

drop rel_w*_D rel_w11_D_21 diff_Drel*
replace faction = 1

forvalue i = 1/31 {
gen rel_w`i'_D = rel_w`i' * faction
}

gen rel_w11_D_21 = rel_w11_D*21
forvalue i = 12/31 {
gen diff_Drel`i'_11 = rel_w`i'_D - rel_w11_D
}

estimate restore DDD
predict yhat1, xb
replace yhat1 = yhat1+rhat

* Prediction if all factions were 0

drop rel_w*_D rel_w11_D_21 diff_Drel*
replace faction = 0

forvalue i = 1/31 {
gen rel_w`i'_D = rel_w`i' * faction
}

gen rel_w11_D_21 = rel_w11_D*21
forvalue i = 12/31 {
gen diff_Drel`i'_11 = rel_w`i'_D - rel_w11_D
}

estimate restore DDD
predict yhat0, xb
replace yhat0 = yhat0+rhat

* Compare total case counts

replace faction = faction_s
gen expyhat0 = exp(yhat0)-1
gen expyhat1 = exp(yhat1)-1

bysort faction treat_csm: sum confn14 expyhat0 expyhat1

bysort faction treat_csm: egen expytot = total(confn14)
bysort faction treat_csm: egen expyhattot0 = total(expyhat0)
bysort faction treat_csm: egen expyhattot1 = total(expyhat1)

bysort faction treat_csm: keep if _n == 1
list faction treat_csm expytot expyhattot0 expyhattot1